/*
通用OTDR

2021.7.28 by Menglong Wu
*/
#include "port.h"
#include "fpga-common.h"
#include "fpga-private.h"
#include "interface/protocal-otdr.h"

extern int env_ignore_rx;


static struct priv_otdr_data data_32k = {
    .cache_len   = 32000 * 4 + 4000,
    .sample_byte = 32 * 1000 * 4,
};

static struct fpga_if otdr_interface_32k = {
    DEFAULT_FPGA_IF,
    .pfun_probe      = cb_fpga_otdr_probe,
    .pfun_release    = cb_fpga_otdr_release,
    .private_data    = &data_32k,
    .name            = "this-otdr-32k",
    .pfun_start      = cb_fpga_otdr_start,
    .pfun_isfinish   = cb_fpga_otdr_isfinish,
    .pfun_stop       = cb_fpga_otdr_stop,
    .pfun_set_gpio   = cb_otdr_set_gpio,
    .pfun_get_gpio   = none_get_gpio,
    .pfun_version    = com_fpga_version,
    .pfun_read_frame = cb_fpga_read_otdr_data,
    .pfun_write_regs = cb_fpga_otdr_write_regs,
};

static struct priv_otdr_data data_64k = {
    .cache_len   = 64000 * 4 + 4000,
    .sample_byte = 64 * 1000 * 4,
};

static struct fpga_if otdr_interface_64k = {
    DEFAULT_FPGA_IF,
    .pfun_probe      = cb_fpga_otdr_probe,
    .pfun_release    = cb_fpga_otdr_release,
    .private_data    = &data_64k,
    .name            = "this-otdr-64k",
    .pfun_start      = cb_fpga_otdr_start,
    .pfun_isfinish   = cb_fpga_otdr_isfinish,
    .pfun_stop       = cb_fpga_otdr_stop,
    .pfun_set_gpio   = cb_otdr_set_gpio,
    .pfun_get_gpio   = none_get_gpio,
    .pfun_version    = com_fpga_version,
    .pfun_read_frame = cb_fpga_read_otdr_data,
    .pfun_write_regs = cb_fpga_otdr_write_regs,
};


extern int cb_fpga_read_otdr_data_orangepi(
	struct fpga_dev *dev,
	unsigned char   *otdr_data,
	unsigned long    len,
	unsigned long   *rlen);
extern int cb_fpga_otdr_write_regs_tg60(struct fpga_dev *dev, unsigned int base_addr, unsigned int *regs, int len);

static struct priv_otdr_data data_32k_orangepi = {
    .cache_len   = 32000 * 4 + 4000,
    .sample_byte = 32 * 1000 * 4,
    .sw_num      = 123,
};

static struct fpga_if otdr_interface_32k_orangepi = {
    DEFAULT_FPGA_IF,
    .pfun_probe      = cb_fpga_otdr_probe,
    .pfun_release    = cb_fpga_otdr_release,
    .private_data    = &data_32k_orangepi,
    .name            = "this-otdr-32k-orangepi",
    .pfun_start      = cb_fpga_otdr_start,
    .pfun_isfinish   = cb_fpga_otdr_isfinish,
    .pfun_stop       = cb_fpga_otdr_stop,
    .pfun_set_gpio   = none_set_gpio,
    .pfun_get_gpio   = none_get_gpio,
    .pfun_version    = com_fpga_version,
    .pfun_read_frame = cb_fpga_read_otdr_data_orangepi,
    // .pfun_write_regs = cb_fpga_otdr_write_regs,
    .pfun_write_regs = cb_fpga_otdr_write_regs_tg60,
};

int cb_fpga_read_otdr_data_4k(
	struct fpga_dev *dev,
	unsigned char   *otdr_data,
	unsigned long    len,
	unsigned long   *rlen);

static struct priv_otdr_data data_32k_android = {
    .cache_len   = 32000 * 4 + 4000,
    .sample_byte = 32 * 1000 * 4,
};
// 内核没有经过修改，也不能以模块形式传递spidev.ko，一次只能读取4K点
static struct fpga_if otdr_interface_32k_raw_kernel = {
    DEFAULT_FPGA_IF,
    .pfun_probe      = cb_fpga_otdr_probe,
    .pfun_release    = cb_fpga_otdr_release,
    .private_data    = &data_32k,
    .name            = "this-otdr-32k-raw-kernel",
    .pfun_start      = cb_fpga_otdr_start,
    .pfun_isfinish   = cb_fpga_otdr_isfinish,
    .pfun_stop       = cb_fpga_otdr_stop,
    .pfun_set_gpio   = cb_otdr_set_gpio,
    .pfun_get_gpio   = none_get_gpio,
    .pfun_version    = com_fpga_version,
    .pfun_read_frame = cb_fpga_read_otdr_data_4k,
    .pfun_write_regs = cb_fpga_otdr_write_regs,
};

static void __attribute__((constructor(INIT_BSP))) this_init()
{
    fpga_if_register(&otdr_interface_32k_raw_kernel);
    fpga_if_register(&otdr_interface_32k_orangepi);
    fpga_if_register(&otdr_interface_32k);
    fpga_if_register(&otdr_interface_64k);
}